package com.yang.utils;
import com.yang.async.annotation.AsyncExec;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.CodeSignature;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
 * <p>
 * AOP工具类
 * </p>
 * @author yang
 * @date 2023/8/17
 */
public class AopUtils {

    /**
     * 根据类的字段名字获取类的字段
     */
    public static Object getFieldValue(Object obj, String name) throws Exception {

        Field[] fields = obj.getClass().getDeclaredFields();

        Object object = null;

        for (Field field : fields) {
            field.setAccessible(true);
            if (field.getName().equalsIgnoreCase(name)) {
                object = field.get(obj);
                break;
            }
        }
        return object;
    }

    /**
     * 获取所有的参数名字和参数值
     */
    public static Map<String, Object> getParamValue(ProceedingJoinPoint joinPoint) {

        Object[] paramValues = joinPoint.getArgs();

        String[] paramNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames();

        Map<String, Object> param = new HashMap<>(paramNames.length);

        for (int i = 0; i < paramNames.length; i++) {
            param.put(paramNames[i], paramValues[i]);
        }
        return param;
    }
}
